import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ContentPanel extends JPanel {

  /**
   * Create the panel
   */
  public ContentPanel(RABID frame) {
    super();
    rabidFrame = frame;
    setBackground(RABID.BACK_COLOR);
    setLayout(new GridBagLayout());

    tabbedPane = new JTabbedPane();
    tabbedPane.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        rabidFrame.updateButtons();
      }
    });

    final GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.fill = GridBagConstraints.BOTH;
    gridBagConstraints.weighty = 1;
    gridBagConstraints.weightx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridx = 0;
    add(tabbedPane, gridBagConstraints);

    final JPanel corpusContainer = new JPanel();
    corpusContainer.setLayout(new GridBagLayout());

    displayScroll = new JScrollPane();
    displayScroll.setAutoscrolls(false);
    final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
    gridBagConstraints_1.fill = GridBagConstraints.BOTH;
    gridBagConstraints_1.weighty = 1;
    gridBagConstraints_1.weightx = 1;
    gridBagConstraints_1.gridy = 0;
    gridBagConstraints_1.gridx = 0;
    corpusContainer.add(displayScroll, gridBagConstraints_1);

    displayContentPane = new JTextPane();
    displayContentPane.setBackground(new Color(240, 240, 255));
    displayScroll.setViewportView(displayContentPane);

    final JPanel statsContainer = new JPanel();
    statsContainer.setLayout(new GridBagLayout());

    final JScrollPane statsScroll = new JScrollPane();
    final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
    gridBagConstraints_2.fill = GridBagConstraints.BOTH;
    gridBagConstraints_2.weighty = 1;
    gridBagConstraints_2.weightx = 1;
    gridBagConstraints_2.gridy = 0;
    gridBagConstraints_2.gridx = 0;
    statsContainer.add(statsScroll, gridBagConstraints_2);

    statsContentPane = new JTextPane();
    statsContentPane.setBackground(new Color(255, 255, 221));
    statsContentPane.setText("Stats content goes here");
    statsScroll.setViewportView(statsContentPane);

    final JPanel indexContainer = new JPanel();
    indexContainer.setLayout(new GridBagLayout());

    final JScrollPane indexScroll = new JScrollPane();
    final GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
    gridBagConstraints_3.fill = GridBagConstraints.BOTH;
    gridBagConstraints_3.weighty = 1;
    gridBagConstraints_3.weightx = 1;
    gridBagConstraints_3.gridy = 0;
    gridBagConstraints_3.gridx = 0;
    indexContainer.add(indexScroll, gridBagConstraints_3);

    indexInfoPane = new JTextPane();
    indexInfoPane.setBackground(new Color(215, 255, 215));
    indexInfoPane.setText("Index info goes here");
    indexScroll.setViewportView(indexInfoPane);

    final JPanel resultsContainer = new JPanel();
    resultsContainer.setLayout(new GridBagLayout());

    final JScrollPane resultsScroll = new JScrollPane();
    final GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
    gridBagConstraints_4.fill = GridBagConstraints.BOTH;
    gridBagConstraints_4.weighty = 1;
    gridBagConstraints_4.weightx = 1.0;
    gridBagConstraints_4.gridy = 0;
    gridBagConstraints_4.gridx = 0;
    resultsContainer.add(resultsScroll, gridBagConstraints_4);

    resultsScroll.setViewportView(rabidFrame.getSearchTreePanel());

    final JPanel browseContainer = new JPanel();
    browseContainer.setLayout(new GridBagLayout());

    final JScrollPane browseScroll = new JScrollPane();
    final GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();
    gridBagConstraints_5.fill = GridBagConstraints.BOTH;
    gridBagConstraints_5.weighty = 1;
    gridBagConstraints_5.weightx = 1;
    gridBagConstraints_5.gridy = 0;
    gridBagConstraints_5.gridx = 0;
    browseContainer.add(browseScroll, gridBagConstraints_5);

    tabbedPane.addTab("Results", null, resultsContainer, null);
    tabbedPane.addTab("Display", null, corpusContainer, null);
    tabbedPane.addTab("Browse", null, browseContainer, null);
    tabbedPane.addTab("Stats", null, statsContainer, null);
    tabbedPane.addTab("Index", null, indexContainer, null);

    browseScroll.setViewportView(rabidFrame.getBrowseTreePanel());
  }

  public String getActiveTabPanelString() {
    int active = tabbedPane.getSelectedIndex();
    return tabbedPane.getTitleAt(active);
  }

  public CorpusFile getCorpusFile() {
    return corpusFile;
  }

  public void selectTab(String title) {
    for (int i = 0, c = tabbedPane.getTabCount(); i < c; ++i) {
      if (tabbedPane.getTitleAt(i).equals(title)) {
        tabbedPane.setSelectedIndex(i);
        break;
      }
    }
  }

  public void setCorpusFile(CorpusFile corpusFile) {
    this.corpusFile = corpusFile;
    displayContentPane.setText("");
    displayContentPane.setText(corpusFile.getContents());
    JScrollBar verticalScrollBar = displayScroll.getVerticalScrollBar();
    // TODO: fix scrolling?
    verticalScrollBar.setValue(verticalScrollBar.getMinimum());
  }

  public void setIndexContent(String index) {
    indexInfoPane.setText("");
    indexInfoPane.setText(index);
  }

  public void setStatsContent(String stats) {
    statsContentPane.setText("");
    statsContentPane.setText(stats);
  }

  private CorpusFile corpusFile;
  private JTextPane displayContentPane;
  private JScrollPane displayScroll;
  private JTextPane indexInfoPane;
  private RABID rabidFrame;
  private JTextPane statsContentPane;

  private JTabbedPane tabbedPane;

}
